#include "calendarwidget.h"
#include <QList>
#include <QDebug>
#include <QTextCharFormat>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>

CalendarWidget::CalendarWidget(QWidget *parent)
{
    setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    hide();
    this->setParent(parent);
    setGeometry(50,50,500,400);
}

CalendarWidget::~CalendarWidget()
{

}

void CalendarWidget::initData()
{
    QSqlQuery query;
    //查询当前工作人员 上班出勤的所有日期
    query.prepare("SELECT time FROM attendance a JOIN user u ON a.userId = u.id WHERE a.type = 1 AND u.username = :username");
    query.bindValue(":username", this->username);
    if (!query.exec())
    {
        qDebug() << query.lastError();
    }
    else
    {
        while(query.next())
        {
            QString datetimeStr = query.value(0).toString();
            QDateTime datetime = QDateTime::fromString(datetimeStr, "yyyy-MM-dd hh:mm:ss");
            QDate date = datetime.date();
            qDebug() << date.toString();
            attendanceDateList.push_back(date);
        }
    }

}

/**
 * @brief 绘制日历
 */
void CalendarWidget::paint()
{
    if (attendanceDateList.isEmpty())
    {
        return;
    }
    foreach (QDate d , attendanceDateList)
    {
        QTextCharFormat format;
        format.setBackground(Qt::green);  //设置格式，颜色自选
        setDateTextFormat(d, format); //生效配置
    }

}

void CalendarWidget::setUserName(QString username)
{
    this->username = username;
    initData();
    paint();
}


